if (typeof gxc == "undefined") {
	var gxc = {};
}

gxc.grid = {};

gxc.grid.create = function(param) {

	// 初始化gxc.grid[gridname]对象
	var gridname = param.name;
	gxc.grid[gridname] = {};
	gxc.grid[gridname].name = gridname;

	// renderTo
	gxc.grid[gridname].renderTo = param.renderTo;

	// url
	gxc.grid[gridname].url = param.url;

	// 列信息
	gxc.grid[gridname].columns = param.columns;

	// 是否立即加载数据
	if (typeof param.loadDataNow != "boolean") {
		gxc.grid[gridname].loadDataNow = true;
	} else {
		gxc.grid[gridname].loadDataNow = param.loadDataNow;
	}

	// 回调函数，在reload数据后执行该函数
	if (typeof param.callback != "function") {
		gxc.grid[gridname].callback = null;
	} else {
		gxc.grid[gridname].callback = param.callback;
	}

	// 显示分页工具栏，默认显示
	if (typeof param.withPagebar == "undefined") {
		gxc.grid[gridname].withPagebar = true;
	} else {
		gxc.grid[gridname].withPagebar = param.withPagebar;
	}

	// 是否有复选框，默认不显示
	if (typeof param.withCheckbox != "boolean") {
		gxc.grid[gridname].withCheckbox = false;
	} else {
		gxc.grid[gridname].withCheckbox = param.withCheckbox;
	}
	// 是否有对应的table的class名称
	if (typeof param.className == "undefined") {
		gxc.grid[gridname].className = null;
	} else {
		gxc.grid[gridname].className = param.className;
	}
	// 是否有序号，默认不显示
	if (typeof param.withRownum != "boolean") {
		gxc.grid[gridname].withRownum = false;
	} else {
		gxc.grid[gridname].withRownum = param.withRownum;
	}
	// 是否隔行换色，默认不换色
	if (typeof param.stripeRows != "boolean") {
		gxc.grid[gridname].stripeRows = false;
	} else {
		gxc.grid[gridname].stripeRows = param.stripeRows;
	}
	// 是否鼠标移上换色，默认不换色
	if (typeof param.stripeOverRow != "boolean") {
		gxc.grid[gridname].stripeOverRow = false;
	} else {
		gxc.grid[gridname].stripeOverRow = param.stripeOverRow;
	}

	// 排序参数
	if (typeof param.fieldSort == "undefined") {
		gxc.grid[gridname].fieldSort = null;
	} else {
		gxc.grid[gridname].fieldSort = param.fieldSort;
	}

	// 分页参数，默认每页20条
	if (typeof param.pageSize != "number") {
		gxc.grid[gridname].pageSize = 20;
	} else {
		gxc.grid[gridname].pageSize = param.pageSize;
	}
	gxc.grid[gridname].pageCurrent = 1;
	gxc.grid[gridname].pageCount = 0;
	gxc.grid[gridname].recordCount = 0;

	// 查询参数
	gxc.grid[gridname].params = {};

	// 设置post请求的参数
	gxc.grid[gridname].postParams = null;
	gxc.grid[gridname].setPostParams = function() {
		this.postParams = null;
		this.postParams = {};
		// 翻页参数
		this.postParams.pageSize = this.pageSize;
		this.postParams.pageCurrent = this.pageCurrent;
		this.postParams.pageCount = this.pageCount;
		this.postParams.recordCount = this.recordCount;
		// 排序参数
		if (this.fieldSort != null) {
			this.postParams.fieldSort = this.fieldSort;
		}
		// 查询参数
		for ( var item in this.params) {
			this.postParams[item] = this.params[item];
		}
		item = null;
	}

	// 显示grid
	gxc.grid[gridname].show = function() {
		// table
		var tableString = "";
		if (this.className == null) {
			tableString = "<div class='gxc_grid_content' style='display:none'><table class='gxc_table'></table>";
		} else {
			tableString = "<table class='gxc_table " + this.className
					+ "'></table></div>";
		}
		jQuery(this.renderTo).append(tableString);
		tableString = null;

		// thead
		var headString = "<thead><tr>";
		if (this.withCheckbox) {
			headString += "<th class='th_checkbox'><input type='checkbox' class='checkbox_all' onchange='gxc.grid."
					+ this.name + ".selectAll()'></th>";
		}
		if (this.withRownum) {
			headString += "<th class='th_rownum'>序号</th>";
		}
		for (i = 0; i < this.columns.length; i++) {
			if (typeof this.columns[i].width != "undefined") {
				headString += "<th width='" + this.columns[i].width + "'>"
						+ this.columns[i].text + "</th>";
			} else {
				headString += "<th>" + this.columns[i].text + "</th>";
			}
		}
		headString += "</tr></thead>";
		jQuery(this.renderTo + " .gxc_table").append(headString);
		headString = null; // 释放变量

		// 分页工具栏
		var pagebar = "<div class='gxc_pagebar' style='display: none;'>";
		pagebar += "<a class='pagebar_item firstpage' href='javascript: gxc.grid."
				+ this.name + ".firstpage()'>首页</a>";
		pagebar += "<a class='pagebar_item previouspage' href='javascript: gxc.grid."
				+ this.name + ".previouspage()'>上页</a>";
		pagebar += "<a class='pagebar_item nextpage' href='javascript: gxc.grid."
				+ this.name + ".nextpage()'>下页</a>";
		pagebar += "<a class='pagebar_item lastpage' href='javascript: gxc.grid."
				+ this.name + ".lastpage()'>末页</a>";

		pagebar += "<span class='pagebar_item'>第"
				+ "<span id='pageCurrent'>0</span>" + "页</span>";
		/*
		 * pagebar += "<span class='pagebar_item'>跳至" + "<input type='text'
		 * id='pageGo' maxlength='8'/>" + "<a href='javascript: gxc.grid." +
		 * this.name + ".gotopage()'>GO</a>" + "</span>";
		 * 
		 * pagebar += "<a class='pagebar_item' href='javascript: gxc.grid." +
		 * this.name + ".refresh()'>刷新</a>";
		 */
		pagebar += "<span class='pagebar_item'>共"
				+ "<span id='pageCount'>0</span>" + "页</span>";

		pagebar += "<span class='pagebar_item'>共"
				+ "<span id='recordCount'>0</span>" + "条记录</span>";

		pagebar += "<span class='pagebar_item'>每页显示";
		pagebar += "<select id='pageSize' onchange='gxc.grid." + this.name
				+ ".changePageSize()'>";
		pagebar += "<option value='10'>10</option>";
		pagebar += "<option value='20'>20</option>";
		pagebar += "<option value='30'>30</option>";
		pagebar += "<option value='40'>40</option>";
		pagebar += "<option value='50'>50</option>";
		pagebar += "</select>";
		pagebar += "条</span>";

		pagebar += "</div>";
		jQuery(this.renderTo).append(pagebar);
		pagebar = null; // 释放变量

		// tbody数据加载
		var bodyString = "<tbody></tbody>";
		jQuery(this.renderTo + " .gxc_table").append(bodyString);
		bodyString = null;
		if (this.loadDataNow) {
			this.reload();
		}
	}

	// 通过ajax获取json
	gxc.grid[gridname].reload = function() {
		var thegrid = this;
		thegrid.setPostParams();
		jQuery.ajax({
			type : "post",
			url : thegrid.url,
			data : thegrid.postParams,
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
				gxc.loading(true);
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					thegrid.reloadData(data.data);
				} else if (data.status == "failed") {
					if (data.data) {
						gxc.dialog.prompt("grid加载数据失败，" + data.data);
					} else {
						gxc.dialog.prompt("grid加载数据失败");
					}
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
				gxc.loading(false);
				thegrid.postParams = null; // 释放
			},
			error : function() {
				gxc.dialog.prompt("grid加载数据失败");
			}
		})
	}

	// 将json数据显示到grid中
	gxc.grid[gridname].reloadData = function(data) {
		var thegrid = this;
		// 取消全选
		if (thegrid.withCheckbox) {
			jQuery(thegrid.renderTo + " .checkbox_all").removeAttr("checked");
		}

		// 刷新翻页信息
		thegrid.pageSize = parseInt(data.pageInfo.pageSize);
		thegrid.pageCurrent = parseInt(data.pageInfo.pageCurrent);
		thegrid.pageCount = parseInt(data.pageInfo.pageCount);
		thegrid.recordCount = parseInt(data.pageInfo.recordCount);
		jQuery(thegrid.renderTo + " #pageSize").val(thegrid.pageSize);
		jQuery(thegrid.renderTo + " #pageCurrent").html(thegrid.pageCurrent);
		jQuery(thegrid.renderTo + " #pageCount").html(thegrid.pageCount);
		jQuery(thegrid.renderTo + " #recordCount").html(thegrid.recordCount);
		// 设置翻页按钮是否可用
		jQuery(thegrid.renderTo + " .firstpage").attr("href",
				"javascript: gxc.grid." + thegrid.name + ".firstpage()");
		jQuery(thegrid.renderTo + " .previouspage").attr("href",
				"javascript: gxc.grid." + thegrid.name + ".previouspage()");
		jQuery(thegrid.renderTo + " .nextpage").attr("href",
				"javascript: gxc.grid." + thegrid.name + ".nextpage()");
		jQuery(thegrid.renderTo + " .lastpage").attr("href",
				"javascript: gxc.grid." + thegrid.name + ".lastpage()");
		if (thegrid.pageCurrent.toString() == "1") {
			jQuery(thegrid.renderTo + " .firstpage").removeAttr("href");
			jQuery(thegrid.renderTo + " .previouspage").removeAttr("href");
		}
		if (thegrid.pageCurrent.toString() == thegrid.pageCount.toString()) {
			jQuery(thegrid.renderTo + " .nextpage").removeAttr("href");
			jQuery(thegrid.renderTo + " .lastpage").removeAttr("href");
		}

		// 刷新tbody内容
		var bodyString = "";
		if (data.list.length <= 0) {
			i = jQuery(thegrid.renderTo + " th").length;
			bodyString += "<tr><td class='td_nodata' colspan='" + i
					+ "'>没有查询到符合条件的记录</td></tr>";
		} else {
			jQuery
					.each(
							data.list,
							function(j, item) {
								if (typeof item.id == "string") {
									bodyString += "<tr id='" + item.id + "'>";
								} else {
									bodyString += "<tr>";
								}
								if (thegrid.withCheckbox) {
									bodyString += "<td class='td_checkbox'><input type='checkbox' class='checkbox_row'></td>";
								}
								if (thegrid.withRownum) {
									bodyString += "<td class='td_rownum'>"
											+ (j + 1).toString() + "</td>";
								}
								for (i = 0; i < thegrid.columns.length; i++) {
									// 是否有td的class名称
									bodyString += "<td";
									if (typeof thegrid.columns[i].className != "undefined") {
										bodyString += " class='"
												+ thegrid.columns[i].className
												+ "'";
									}
									if (typeof thegrid.columns[i].align != "undefined") {
										bodyString += " align='"
												+ thegrid.columns[i].align
												+ "'";
									}
									bodyString += ">";
									// 是否有render函数
									if (typeof thegrid.columns[i].render != "undefined") {
										bodyString += thegrid.columns[i]
												.render(item)
												+ "</td>";
									} else {
										if (item[thegrid.columns[i].name]) {
											bodyString += item[thegrid.columns[i].name];
										}
										bodyString += "</td>";
									}
								}
								bodyString += "</tr>"
							})
		}
		jQuery(thegrid.renderTo + " .gxc_table tbody").html("");
		jQuery(thegrid.renderTo + " .gxc_table tbody").html(bodyString);
		bodyString = null; // 释放变量

		// 隔行换色
		if (thegrid.stripeRows) {
			jQuery(thegrid.renderTo + " .gxc_table tr:even").addClass("alt");
		}
		// 鼠标移上换色
		if (thegrid.stripeOverRow) {
			jQuery(thegrid.renderTo + " .gxc_table tr").mouseover(function() {
				jQuery(this).addClass("over");
			})
			jQuery(thegrid.renderTo + " .gxc_table tr").mouseout(function() {
				jQuery(this).removeClass("over");
			})
		}

		// 显示
		jQuery(this.renderTo + " .gxc_grid_content").show();
		if (this.withPagebar) {
			jQuery(this.renderTo + " .gxc_pagebar").show();
		}

		// 回调函数
		if (thegrid.callback != null) {
			thegrid.callback();
		}
	}

	// 显示第一页
	gxc.grid[gridname].firstpage = function() {
		this.pageCurrent = 1;
		this.reload();
	}

	// 上一页
	gxc.grid[gridname].previouspage = function() {
		if (this.pageCurrent > 1) {
			this.pageCurrent = parseInt(this.pageCurrent) - parseInt(1);
		}
		this.reload();
	}

	// 下一页
	gxc.grid[gridname].nextpage = function() {
		if (this.pageCurrent < this.pageCount) {
			this.pageCurrent = parseInt(this.pageCurrent) + parseInt(1);
		}
		this.reload();
	}

	// 最后一页
	gxc.grid[gridname].lastpage = function() {
		this.pageCurrent = this.pageCount;
		this.reload();
	}

	// 跳转到指定页
	gxc.grid[gridname].gotopage = function() {
		var pageGo = jQuery(this.renderTo + " #pageGo").val();
		pageGo = parseInt(pageGo);
		if (isNaN(pageGo)) {
			return;
		}
		this.pageCurrent = pageGo;
		this.reload();
	}

	// 调整每页显示条数
	gxc.grid[gridname].changePageSize = function() {
		this.pageSize = jQuery(this.renderTo + " #pageSize").val();
		this.pageCurrent = 1;
		this.pageCount = 0;
		this.reload();
	}

	// 刷新
	gxc.grid[gridname].refresh = function() {
		this.reload();
	}

	// 全选/全不选
	gxc.grid[gridname].selectAll = function() {
		if (jQuery(this.renderTo + " .checkbox_all").prop("checked")) {
			jQuery(this.renderTo + " .checkbox_row").prop("checked", true);
		} else {
			jQuery(this.renderTo + " .checkbox_row").prop("checked", false);
		}
	}

	// 获取选中行的id，用逗号隔开
	gxc.grid[gridname].getSelectedRows = function() {
		var id_array = new Array();
		jQuery(this.renderTo + " .checkbox_row").each(function(i) {
			if (this.checked == true) {
				id_array.push(jQuery(this).parent().parent().attr("id"));
			}
		});
		return id_array;
	}
	// 加载grid到页面中
	gxc.grid[gridname].show();
}

// 加载状态
gxc.loading = function(b) {
	if (b) {
		var htmlString = "<div class='gxc_loading_bg'></div><div class='gxc_loading_img'></div>";
		jQuery("body").append(htmlString);
	} else {
		jQuery(".gxc_loading_bg").remove();
		jQuery(".gxc_loading_img").remove();
	}
}